package com.sskj.gcc.ui.fragment;


import android.graphics.PointF;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.davemorrissey.labs.subscaleview.ImageSource;
import com.davemorrissey.labs.subscaleview.ImageViewState;
import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView;
import com.sskj.common.rxbus2.RxBus;
import com.sskj.gcc.R;
import com.sskj.gcc.presenter.KePuFragmentPresenter;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseFragment;

import java.io.File;

import butterknife.BindView;

@Route(path = RConfig.APP_FRAGMENT_KEPU)
public class KePuFragment extends BaseFragment<KePuFragmentPresenter> {

    @BindView(R.id.iv_kepu)
    SubsamplingScaleImageView ivKepu;

    @Override
    protected int getLayoutId() {
        return R.layout.app_fragment_kepu;
    }

    @Override
    public KePuFragmentPresenter getPresenter() {
        return new KePuFragmentPresenter();
    }

    @Override
    public void initView() {
        RxBus.getDefault().register(this);
        ARouter.getInstance().inject(this);
    }

    @Override
    public void initData() {
        mPresenter.getKePu();
    }


    public void updateUI(String data) {
        if (ivKepu != null)
            if (!TextUtils.isEmpty(data)) {
                data = data.replace("\\/", "/");

                ivKepu.setMinimumScaleType(SubsamplingScaleImageView.SCALE_TYPE_CENTER_CROP);
                ivKepu.setMinScale(0.5F);//最小显示比例
                ivKepu.setMaxScale(10.0F);//最大显示比例（太大了图片显示会失真，因为一般微博长图的宽度不会太宽）

                //下载图片保存到本地
                Glide.with(this)
                        .load(data).downloadOnly(new SimpleTarget<File>() {
                    @Override
                    public void onResourceReady(@NonNull File resource, @Nullable Transition<? super File> transition) {
                        // 将保存的图片地址给SubsamplingScaleImageView,这里注意设置ImageViewState设置初始显示比例
                        ivKepu.setImage(ImageSource.uri(Uri.fromFile(resource)), new ImageViewState(1.0F, new PointF(0, 0), 0));
                    }
                });


            }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

    }
}
